package v3

import "fmt"

func (cli *CLI) createBlockChain(address string) {
	NewBlockChain(address)
	fmt.Println("create blockchain successfully!")
}

func (cli *CLI) addBlock(data string) {
	blockChain := GetBlockChain()
	if blockChain == nil {
		fmt.Println("addBlock fail GetBlockChain !")
		return
	}
	defer blockChain.DB.Close()
	blockChain.AddBlock(data)
	fmt.Println("addBlock successfully!")
}

func (cli *CLI) printChain() {
	blockChain := GetBlockChain()
	if blockChain == nil {
		fmt.Println("printChain fail GetBlockChain !")
		return
	}
	defer blockChain.DB.Close()
	var it = BlockChainIterator{
		blockChain.DB,
		blockChain.TailHash,
	}
	for {
		block := it.Next()
		fmt.Println(" ===========================================================")
		fmt.Printf("Version : %d\n", block.Version)
		fmt.Printf("PrevBlockHash : %x\n", block.PreHash)
		fmt.Printf("Hash : %x\n", block.Hash)
		fmt.Printf("MerkleRoot : %x\n", block.MerkleRoot)
		fmt.Printf("TimeStamp : %d\n", block.Timestamp)
		fmt.Printf("Difficuty : %d\n", block.Difficulty)
		fmt.Printf("Nonce : %d\n", block.Nonce)
		fmt.Printf("Data : %s\n", block.Data)
		if len(block.PreHash) == 0 {
			fmt.Println("打印所有区块完成!")
			break
		}
	}
}
